//If skeletal animations
#ifdef SKELETAL

#define BONECOUNT 40
attribute highp vec4 aBoneWeight;
attribute highp vec4 aBoneIndex;  
      
//Quaternion with the rotation
uniform highp vec4 uBoneTransformsQR[BONECOUNT];
//Quaternion with the translation
uniform highp vec4 uBoneTransformsQT[BONECOUNT];
   
#endif

//The equivalent in the js code is in Shader.js
attribute highp vec3 aVertexPosition;
attribute highp vec2 aTextureCoord;
attribute highp vec3 aNormalPosition;
 
//Uniforms
uniform highp mat4 uWorldMatrix;	
uniform highp mat4 uViewMatrix;
uniform highp mat4 uProjMatrix;

//Varying variables
varying highp vec4 m_position;
varying highp vec2 vTextureCoord;
        
varying highp vec4 m_worldpos;
//worldspaceNormal
varying highp vec3 m_wsnormal;      
   
//Vertex Shader
void main(void) 
{   
	#ifdef SKELETAL
		//Add the weights from x,y,z,w
		vec4 f_blendDQR = aBoneWeight.x * uBoneTransformsQR[int(aBoneIndex.x)];
		vec4 f_blendDQT = aBoneWeight.x * uBoneTransformsQT[int(aBoneIndex.x)];

		f_blendDQR += aBoneWeight.y * uBoneTransformsQR[int(aBoneIndex.y)];
		f_blendDQT += aBoneWeight.y * uBoneTransformsQT[int(aBoneIndex.y)];

		f_blendDQR += aBoneWeight.z * uBoneTransformsQR[int(aBoneIndex.z)];
		f_blendDQT += aBoneWeight.z * uBoneTransformsQT[int(aBoneIndex.z)];

		f_blendDQR += aBoneWeight.w * uBoneTransformsQR[int(aBoneIndex.w)];
		f_blendDQT += aBoneWeight.w * uBoneTransformsQT[int(aBoneIndex.w)];
		//Normalize the dq
		float len = length(f_blendDQR);
		f_blendDQR /= len;
		f_blendDQT /= len;
		//The object means object space
		vec3 f_objectpos = aVertexPosition + 2.0 * cross(f_blendDQR.xyz, cross(f_blendDQR.xyz, aVertexPosition) + f_blendDQR.w * aVertexPosition);
		//The translation part
		vec3 f_trans =  2.0*(f_blendDQR.w * f_blendDQT.xyz - f_blendDQT.w * f_blendDQR.xyz + cross(f_blendDQR.xyz, f_blendDQT.xyz)) ;
		f_objectpos += f_trans;

		vec3 f_objectNormal = aNormalPosition + 2.0 * cross(f_blendDQR.xyz, cross(f_blendDQR.xyz, aNormalPosition) + f_blendDQR.w * aNormalPosition);

		m_worldpos = uWorldMatrix * vec4(f_objectpos, 1.0); 
		highp vec4 f_wsnormals = uWorldMatrix * vec4(f_objectNormal, 1.0);                          	
	#else
		m_worldpos = uWorldMatrix * vec4( aVertexPosition, 1.0);
		highp vec4 f_wsnormals = uWorldMatrix * vec4(aNormalPosition, 1.0);   
	#endif

	m_position = uProjMatrix * uViewMatrix * m_worldpos;
	         
	m_wsnormal = normalize(f_wsnormals.xyz);

	vTextureCoord = aTextureCoord;

	gl_Position = m_position;            
}